package dao

import (
	"github.com/jinzhu/gorm"
	"time"
)

type Bj struct {

	Uid int `json:"uid" gorm:"column:uid"`
	Name string `json:"name" gorm:"column:name"`
	Bjid int `json:"bj_id" gorm:"column:bj_id"`
	ImgId int `json:"img_id" gorm:"column:img_id"`
	InsId int `json:"ins_id" gorm:"column:ins_id"`
	Desc string `json:"desc" gorm:"column:desc"`
	UserName string `json:"username" gorm:"column:username"`
	Password string `json:"password" gorm:"column:password"`
	Ip string `json:"ip" gorm:"column:ip"`
	Subnet string `json:"subnet" gorm:"column:subnet"`
	Gateway string `json:"gateway" gorm:"column:gateway"`
	Tag string `json:"tag" gorm:"column:tag"`
	Ctime int64 `json:"c_time" gorm:"column:c_time"`
	Dtime int64 `json:"d_time" gorm:"column:d_time"`

}

type BjAndPlatform struct {
	Bj
	Platform string `json:"platform"`
	Nickname string `json:"nickname" gorm:"column:nickname"`
}

func (t *Bj)Create(data map[string]interface{}) error{

	var bj Bj
	if v,ok := data["uid"];ok{
		bj.Uid = v.(int)
	}
	if v,ok := data["name"];ok{
		bj.Name = v.(string)
	}
	if v,ok := data["img_id"];ok{
		bj.ImgId = v.(int)
	}
	if v,ok := data["ins_id"];ok{
		bj.InsId = v.(int)
	}
	if v,ok := data["desc"];ok{
		bj.Desc = v.(string)
	}
	if v,ok := data["username"];ok{
		bj.UserName = v.(string)
	}
	if v,ok := data["password"];ok{
		bj.Password = v.(string)
	}
	if v,ok := data["ip"];ok{
		bj.Ip = v.(string)
	}
	if v,ok := data["subnet"];ok{
		bj.Subnet = v.(string)
	}
	if v,ok := data["gateway"];ok{
		bj.Gateway = v.(string)
	}
	if v,ok := data["tag"];ok{
		bj.Tag = v.(string)
	}
	if v,ok := data["uid"];ok{
		bj.Uid = v.(int)
	}
	bj.Ctime = time.Now().Unix()
	err := db.Create(&bj).Error
	if err != nil {
		return err
	}
	return nil

}
func (t *Bj)Delete(where map[string]interface{}) error {

	upd := Bj{
		Dtime:time.Now().Unix(),
	}
	err := db.Model(&t).Where(where).Update(upd).Error
	if err != nil{
		return err
	}
	return nil

}
func (t *Bj)Update(where map[string]interface{},data map[string]interface{}) error {

	var bj Bj

	if v,ok := data["name"];ok{
		bj.Name = v.(string)
	}
	if v,ok := data["img_id"];ok{
		bj.ImgId = v.(int)
	}
	if v,ok := data["ins_id"];ok{
		bj.InsId = v.(int)
	}
	if v,ok := data["desc"];ok{
		bj.Desc = v.(string)
	}
	if v,ok := data["username"];ok{
		bj.UserName = v.(string)
	}
	if v,ok := data["password"];ok{
		bj.Password = v.(string)
	}
	if v,ok := data["ip"];ok{
		bj.Ip = v.(string)
	}
	if v,ok := data["subnet"];ok{
		bj.Subnet = v.(string)
	}
	if v,ok := data["gateway"];ok{
		bj.Gateway = v.(string)
	}
	if v,ok := data["tag"];ok{
		bj.Tag = v.(string)
	}

	err := db.Model(&t).Where(where).Updates(&bj).Error
	if err != nil{
		return err
	}
	return nil

}
func (t *Bj)Find(w map[string]interface{},s map[string]interface{},page int,pageSize int) ([]*BjAndPlatform,int,error) {

	var total int = 0
	var bj []*BjAndPlatform
	// 获取取指page，指定pagesize的记录
	table := db.Table("bc_bj").
		Where(w).
		Where("bc_bj.d_time = ?",0).
		Joins("left join bc_image as image on bc_bj.img_id=image.img_id").
		Joins("left join bc_user as user on bc_bj.uid=user.uid")
	//长度为4 则为高级全部模糊查询，需要 (xx) and (xx or xx)查询
	if len(s) == 4 {
		table = table.Where("nickname like ? or image.name like ? or vir_type like ? or platform like ?",
			"%" +  s["nickname"].(string) + "%",
			"%" +  s["vm_name"].(string) + "%",
			"%" +  s["vir_type"].(string) + "%",
			"%" +  s["platform"].(string) + "%",
		)
	}else{
		//制作者
		if v,ok := s["nickname"];ok {
			table = table.Where("nickname like ?","%" +  v.(string) + "%")
		}
		//镜像名称
		if v,ok := s["vm_name"];ok {
			table = table.Where("image.name like ?", "%" +  v.(string) + "%")
		}
		//虚拟机类型
		if v,ok := s["vir_type"];ok {
			table = table.Where("vir_type like ?", "%" +  v.(string) + "%")
		}
		//系统类型
		if v,ok := s["platform"];ok {
			table = table.Where("platform like ?", "%" +  v.(string) + "%")
		}
	}

	err := table.Select("bc_bj.*,platform,nickname").
		Limit(pageSize).Offset((page-1)*pageSize).
		Order("bj_id desc").Find(&bj).Error
	if err != nil && err != gorm.ErrRecordNotFound{
		return nil ,0,err
	}


	// 获取总条数
	table.Count(&total)

	if err != nil && err != gorm.ErrRecordNotFound{
		return nil ,0,err
	}
	return bj,total,nil

}
func (t *Bj)First(bj_id int) (error, *Bj) {

	err :=db.Where("bj_id = ?",bj_id).First(&t).Error

	if err != nil{
		return err,nil
	}
	if t.Bjid > 0{
		return err,t
	}
	return nil,t

}

var BjDao = &Bj{}

type BjImgAndIns struct {
	ImgUUId string `json:"img_uuid" gorm:"column:uuid"`
	InsUUId string `json:"ins_uuid" gorm:"column:resource_uuid"`
}

func (t *Bj)ImgAndInsUUid(bjId int) (error, BjImgAndIns) {
	var bjImgAndIns BjImgAndIns
	table := db.Table("bc_bj b")
	err := table.Joins("left join bc_image i on i.img_id=b.img_id").
		Select("b.bj_id,i.uuid,o.resource_uuid").
		Joins("left join bc_instance_offering o on o.io_id=b.ins_id").
		Where("b.bj_id=?", bjId).
		First(&bjImgAndIns)
	if err != nil {
		return err.Error,bjImgAndIns
	}
	return  nil, bjImgAndIns
}